<template>
  <div class="app-container customer-div">
    <div style="margin-top: 20px">
      <!--搜索-->
      <el-form :inline="true" :model="params" size="mini" label-width="100px">
        <el-form-item label="订单日期：">
          <el-date-picker
            v-model="params.createDate"
            type="daterange"
            format="yyyy 年 MM 月 dd 日"
            range-separator="至"
            start-placeholder="开始日期"
            end-placeholder="结束日期">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="订单编号：">
          <el-input v-model="params.orderSn"></el-input>
        </el-form-item>
        <el-form-item label="客户名称：">
          <el-select
            clearable
            v-model="params.customerId"
            filterable
            remote
            reserve-keyword
            placeholder="请输入关键词"
            :remote-method="remoteMethod1"
            :loading="customerLoading">
            <el-option
              v-for="item in customerList"
              :key="item.customerId"
              :label="item.customerName"
              :value="item.customerId">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="销售员：">
          <el-select v-model="params.saleUserId" filterable clearable >
            <el-option
              v-for="item in userOptionList"
              :key="item.userId"
              :label="item.userName"
              :value="item.userId">
            </el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="采购员：">
          <el-select v-model="params.purchaseUserId" clearable >
            <el-option
              v-for="item in userOptionList2"
              :key="item.userId"
              :label="item.userName"
              :value="item.userId">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="调整状态：">
          <el-select v-model.number="params.isCustomerTypeChange" class="input-width" style="width:195px" placeholder="请选择" clearable>
            <el-option :value="1" label="已调整" :key="1"></el-option>
            <el-option :value="0" label="未调整" :key="0"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button
            @click="handleSearchList()"
            type="primary">
            查询
          </el-button>
        </el-form-item>
      </el-form>
    </div>
    <!--数据列表-->
    <div class="table-container">
      <el-table
        v-loading="loading"
        :data="list"
        border
        style="width: 100%; margin-top: 20px">
        <el-table-column
          prop="orderSn"
          label="订单编号"
          align="center"
          min-width="150">
            <template slot-scope="scope">
               <el-link @click="toOrder(scope.row)" style="color:#409EFF">{{scope.row.orderSn}}</el-link>
            </template>
        </el-table-column>
        <el-table-column
          prop="customerName"
          label="客户名称"
          align="center"
          min-width="300">
        </el-table-column>
        <el-table-column
          align="center"
          prop="saleUserName"
          label="销售员"
          min-width="100">
        </el-table-column>

        <el-table-column
          prop="createTime"
          label="订单时间"
          align="center"
          min-width="100">
        </el-table-column>
        <el-table-column
          label="调整状态"
          align="center"
          min-width="100">
          <template slot-scope="scope">
            {{scope.row.isCustomerTypeChange == 1 ? '已调整' : '未调整'}}
          </template>
        </el-table-column>
        <el-table-column
          prop="orderCustomerType"
          label="订单分类"
          align="center"
          min-width="120">
        </el-table-column>
        <el-table-column
          prop="oldOrderCustomerType"
          label="订单分类(销售)"
          align="center"
          min-width="120">
        </el-table-column>
        <el-table-column
          class-name="nopadding"
          label="采购员"
          align="center"
          min-width="100">
          <template slot-scope="scope">
            <div v-for="(item,index) in scope.row.ordGoodsList" :style="{borderTop:index == 0 ? '0' : '1px solid #EBEEF5'}">{{item.purchaseUserName}}</div>
          </template>
        </el-table-column>
        <el-table-column
          class-name="nopadding"
          align="center"
          label="订单分类（采购）"
          min-width="120">
          <template slot-scope="scope">
              <div v-for="(item,index) in scope.row.ordGoodsList" :style="{borderTop:index == 0 ? '0' : '1px solid #EBEEF5'}">
                {{item.purchaseCustomerType}}
              </div>
          </template>
        </el-table-column>
        <el-table-column
          class-name="nopadding"
          fixed="right"
          align="center"
          label="操作"
          min-width="120">
          <template slot-scope="scope">
            <div v-for="(item,index) in scope.row.ordGoodsList" :style="{borderTop:index == 0 ? '0' : '1px solid #EBEEF5'}">
              <el-button
                v-if="customerTypeOrderChange && item.isCanChange == 1"
                type="text"
                size="mini"
                @click="changeOrder(scope.row.orderId,item.purchaseCustomerType)">调整
              </el-button>
            </div>
          </template>
        </el-table-column>


      </el-table>
    </div>

    <div class="pagination-container">
      <el-pagination
        background
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        layout="total, sizes,prev, pager, next,jumper"
        :current-page.sync="params.pageNum"
        :page-size="params.pageSize"
        :page-sizes="[10,15,20]"
        :total="count">
      </el-pagination>
    </div>
  </div>
</template>

<script>
import { getOrderUsersList,getCustomerOption} from '@/api/partner'
import {customerTypeOrderList,editOrderCustomerType} from '@/api/allInLine/inquiry'

import {formatDate} from '@/utils/date';
import store from '@/store'
import { mapGetters } from 'vuex'

export default {
  name: "customerTypeOrderList",
  data(){
    return{
      roles:store.getters.roles ? store.getters.roles : [],
      params:{
        pageNum:1,
        pageSize:10,
        orderSn:'',
        customerId:'',
        saleUserId:'',
        purchaseUserId:'',
        createDate:[],
        isCustomerTypeChange:'',
      },
      list:[],
      count:0,
      loading:false,
      userOptionList:[],
      userOptionList2:[],
      customerList:[],
      customerLoading:false
    }
  },
  computed: {
    ...mapGetters([
      'name',
      'mobile',
      'userId'
    ]),
    customerTypeOrderChange() {
      if(this.roles.indexOf("customerTypeOrderChange") != -1) {
        return true
      } else {
        return false
      }
    },

  },
  created() {
    this.getUserOptionList()
    this.getUserOptionList2();
    this.getListData();
  },
  filters: {

  },
  methods:{
    toOrder(item){
      this.$router.push({name:'saleDetail',query:{id:item.orderId,type:'see'}})
    },
    changeOrder(orderId,purchaseCustomerType){
      this.$confirm('确定调整吗?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        editOrderCustomerType({orderId:orderId,orderCustomerType:purchaseCustomerType}).then(()=>{
          this.getListData();
          this.$message({
            type: 'success',
            message: '操作成功!'
          });
        });
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消操作'
        });
      });
    },
    //获取客户列表
    remoteMethod1(query) {
      if(query && query!= ''){
        this.customerLoading = true;
        getCustomerOption({keywords:query}).then(response => {
          this.customerList = response;
          this.customerLoading = false;
        }).catch(()=>{
          this.customerLoading = false;
        });
      }
    },
    //获取业务员下拉选项
    getUserOptionList(){
      getOrderUsersList({type:'sale'}).then((res)=>{
        this.userOptionList = res;
      })
    },
    //获取业务员下拉选项
    getUserOptionList2(){
      getOrderUsersList({departmentId: 3,status:1}).then((res)=>{
        this.userOptionList2 = res;
      })
    },
    handleSearchList(){
      this.params.pageNum = 1;
      this.getListData();
    },

    handleSizeChange(val){
      this.params.pageNum = 1;
      this.params.pageSize = val;
      this.getListData();
    },
    handleCurrentChange(val){
      this.params.pageNum = val;
      this.getListData();
    },

    getListData(){
      this.loading = true;
      customerTypeOrderList(this.params).then((res)=>{
        this.list = res.list;
        this.count = res.count;
        this.loading = false;
      }).catch(()=>{
        this.loading = false;
      })
    },

  },
}
</script>

<style scoped lang="scss">
.avatar-uploader{
  margin-bottom:10px;
  display: inline-block;
  width:80px;
  height:80px;
  border:1px solid #DCDFE6;
  text-align: center;
  line-height: 80px;
}
</style>
